package org.dromara.study.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.study.domain.bo.EmployeeBo;
import org.dromara.study.domain.entity.Employee;
import org.dromara.study.domain.entity.EmployeeCourse;
import org.dromara.study.domain.entity.EmployeeSkill;
import org.dromara.study.domain.vo.EmployeeVo;
import org.dromara.study.mapper.EmployeeCourseMapper;
import org.dromara.study.mapper.EmployeeMapper;
import org.dromara.study.mapper.EmployeeSkillMapper;
import org.dromara.study.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private EmployeeSkillMapper employeeSkillMapper;

    @Autowired
    private EmployeeCourseMapper employeeCourseMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveAll(EmployeeBo employeeBo) {
        employeeMapper.insert(employeeBo);

        List<EmployeeSkill> employeeSkillList = new ArrayList<>();
        List<Long> skillIds = employeeBo.getSkillIds();
        for (Long skillId : skillIds) {
            employeeSkillList.add(new EmployeeSkill(employeeBo.getId(), skillId));
        }
        employeeSkillMapper.insertMultiple(employeeSkillList);

        List<Long> courseIds = employeeBo.getCourseIds();
        List<EmployeeCourse> employeeCourseList = new ArrayList<>();
        for (Long courseId : courseIds) {
            employeeCourseList.add(new EmployeeCourse(employeeBo.getId(), courseId));
        }

        employeeCourseMapper.insertMultiple(employeeCourseList);

    }

    @Override
    public Page<EmployeeVo> getList(PageQuery pageQuery, EmployeeVo employeeVo) {
        return employeeMapper.getList(pageQuery.build(), employeeVo);
    }
}
